const app = getApp();
const { request } = require('../../../utils/request');

Page({
    data: {
        address: {}
    },

    onLoad(options) {
        const address = JSON.parse(decodeURIComponent(options.address));
        this.setData({ address });
    },

    onNameInput(e) {
        this.setData({
            'address.contact_name': e.detail.value
        });
    },

    onPhoneInput(e) {
        this.setData({
            'address.contact_phone': e.detail.value
        });
    },

    onAddressInput(e) {
        this.setData({
            'address.full_address': e.detail.value
        });
    },

    async submitEdit() {
        console.log('编辑地址数据:', this.data.address); // 打印编辑地址数据

        try {
            // 调用编辑地址接口
            const res = await request({
                url: `/address/${this.data.address.id}`,
                method: 'PUT',
                data: this.data.address,
                showLoading: true
            });

            console.log('编辑地址接口响应:', res); // 打印接口响应

            if (res.code === 2000) {
                wx.navigateBack();
                wx.showToast({
                    title: '修改成功',
                    icon: 'success'
                });
            } else {
                wx.showToast({
                    title: `修改失败: ${res.message || '未知错误'}`,
                    icon: 'none'
                });
                console.error('修改地址失败:', res.message);
            }
        } catch (error) {
            wx.showToast({
                title: `请求出错: ${error.message || '未知错误'}`,
                icon: 'none'
            });
            console.error('修改地址请求出错:', error);
        }
    }
});
